// 详细商品api请求
import {reqgetGoods,reqAddCart} from '@/api/index'
import {getUUID} from '@/utils/uuid_token'
const state = {
  goods:{},
  uuid:getUUID()
}

const getters = {
  categoryView(state){
    // 避免出现undefined,因为传递数据时异步api没执行此时goods为空，没有categoryView属性,就会出现undefined
    return state.goods.categoryView || {}
  },
  skuInfo(state){
    return state.goods.skuInfo || {}
  },
  // 商品售卖属性列表
  spuSaleAttrList(state){
    return state.goods.spuSaleAttrList || []
  }
}

const mutations = {
  // 详细商品
  GETGOODS(state, data) {
    state.goods = data
  }
}

const actions = {
  // 详细商品
  async getGoods(context, id) {
    const res = await reqgetGoods(id)
    if (res.code === 200) {
      context.commit('GETGOODS',res.data)
    }
  },
  // 添加购物车
  async addCart(context, {skuId,skuNum}) {
    const res = await reqAddCart(skuId,skuNum)
    if (res.code === 200) {
      return 'ok'
    }else{
      return Promise.reject(new Error('fail'))
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}